﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;

namespace Golf.WebUI.AdminShow.Ajax
{
    public partial class Login_Ajax : System.Web.UI.Page
    {
        #region 加载判断
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                switch (Request.Form["action"])
                {
                    case "login":
                        Login();
                        break;
                    case "check":
                        CheckName();
                        break;

                }
            }
        }
        #endregion

        #region 登录验证
        private void Login()
        {
            Object code = CookiesHelper.GetCookie("_AdminLogin_Code");
            if (code != null)
            {
                string rCode = Server.UrlDecode(Request.Form["code"]);
                if (code.ToString().ToUpper().Equals(rCode.ToUpper()) == true)
                {
                    Golf.Model.Golf_Admin model = new Golf.Model.Golf_Admin();
                    Golf.BLL.Golf_Admin bll = new Golf.BLL.Golf_Admin();
                    List<Golf.Model.Golf_Admin> list = bll.GetModelList("adminName='" + Server.UrlDecode(Request.Form["name"]) + "'");
                    foreach (var item in list)
                    {
                        model = item;
                    }
                    if (model != null)
                    {
                        if (model.PassWord.ToString().Equals(ThumNail.Lower32(Server.UrlDecode(Request.Form["pwd"]))) == true)
                        // if (model.PassWord.ToString().Equals(Server.UrlDecode(Request.Form["pwd"])) == true)
                        {
                            //model.LoginIP = Request.UserHostAddress;
                            model.LoginIP = ThumNail.GetIp(Request);//获取ip
                            model.LoginNumber = model.LoginNumber + 1;
                            model.LoginTime = DateTime.Now;
                            model.IsState = true;
                            bll.Update(model);
                            CookiesHelper.SaveCookie("_AdminLogin", Server.UrlEncode(model.RealName.ToString()), 7, "D");//保存登录管理员的姓名
                            CookiesHelper.SaveCookie("_AdminID", Server.UrlEncode(model.Id.ToString()), 7, "D");//保存登录管理员的id值
                            Response.Write("3");//登录成功
                        }
                        else
                        {
                            Response.Write("2");//密码错误
                        }
                    }
                    else
                    {
                        Response.Write("1");//用户名错误
                    }
                }
                else
                {
                    Response.Write("0");//验证码错误
                }
            }
        }
        #endregion


        #region 验证帐号
        private void CheckName()
        {
            Golf.BLL.Golf_Admin bll = new Golf.BLL.Golf_Admin();
            DataSet ds = new DataSet();
            ds = bll.GetList(Server.UrlDecode(" AdminName='" + Request.Form["name"]) + "'");
            if (ds.Tables[0].Rows.Count > 0)
            {
                Response.Write("0");//此用户名已经存在,请重新输入
            }
            else
            {
                Response.Write("1");//OK
            }
        }
        #endregion


    }
}
